﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Admin_quan_ly_binh_luan : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadResIntoResDDL();
            LoadPostIntoPostDDL();
            GetAllPostComment();
            GetAllResComment();
        }
    }
    protected void LoadResIntoResDDL()
    {
        try {
            SQLHelper Dal = new SQLHelper();
            Dal.CommandType = CommandType.StoredProcedure;
            Dal.CommandText = "SP_GetAllRes";
            DataTable db = Dal.GetDataTable();
            ddlRes.DataSource = db;
            ddlRes.DataTextField = db.Columns["TenNhaHang"].ToString();
            ddlRes.DataValueField = db.Columns["NhaHangID"].ToString();
            ddlRes.DataBind();
        }
        catch { }
    }
    protected void LoadPostIntoPostDDL()
    { 
        try{
            SQLHelper Dal = new SQLHelper();
            Dal.CommandType = CommandType.StoredProcedure;
            Dal.CommandText = "SP_GetAllPosts";
            DataTable db = Dal.GetDataTable();
            ddlPost.DataSource = db;
            ddlPost.DataTextField = db.Columns["TieuDe"].ToString();
            ddlPost.DataValueField = db.Columns["BaiVietID"].ToString();
            ddlPost.DataBind();
        }
        catch{}
    }
    protected void GetAllPostComment()
    {
        try {
            SQLHelper Dal = new SQLHelper();
            Dal.CommandType = CommandType.StoredProcedure;
            Dal.CommandText = "SP_GetAllPostComment";
            DataTable db = Dal.GetDataTable();
            grvPostComment.DataSource = db;
            grvPostComment.DataBind();
        }
        catch { }
    }
    protected void GetAllResComment()
    {
        try
        {
            SQLHelper Dal = new SQLHelper();
            Dal.CommandType = CommandType.StoredProcedure;
            Dal.CommandText = "SP_GetAllResComment";
            DataTable db = Dal.GetDataTable();
            grvResComment.DataSource = db;
            grvResComment.DataBind();
        }
        catch { }
    }
    protected void GetPostCommentByPostID()
    {
        
        try {
            SQLHelper Dal = new SQLHelper();
            Dal.CommandType = CommandType.StoredProcedure;
            Dal.CommandText = "SP_GetPostCommentByPostID";
            Dal.ParameterCollection = new string[1] { "@BaiVietID" };
            Dal.ValueCollection = new string[1] { ddlPost.SelectedValue};
            DataTable db = Dal.GetDataTable();
            grvPostComment.DataSource = db;
            grvPostComment.DataBind();
        }
        catch { }
    }
    protected void GetResCommentByResID()
    {
        try
        {
            SQLHelper Dal = new SQLHelper();
            Dal.CommandType = CommandType.StoredProcedure;
            Dal.CommandText = "SP_GetResCommentByResID";
            Dal.ParameterCollection = new string[1] { "@NhaHangID" };
            Dal.ValueCollection = new string[1] { ddlRes.SelectedValue };
            DataTable db = Dal.GetDataTable();
            grvResComment.DataSource = db;
            grvResComment.DataBind();
        }
        catch { }
    }
    protected void grvResComment_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        grvResComment.EditIndex = -1;
        GetAllResComment();
    }
    protected void grvResComment_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string i = Convert.ToString(grvResComment.DataKeys[e.RowIndex].Value);
        try {
            SQLHelper Dal = new SQLHelper();
            Dal.Open();
            Dal.CommandType = CommandType.StoredProcedure;
            Dal.CommandText = "SP_DeleteResComment";
            Dal.ParameterCollection = new string[1] { "@BinhLuanID" };
            Dal.ValueCollection = new string[1] { i };
            Dal.ExecuteNonQuery();
            Dal.Close();
            Dal.Dispose();
            GetAllResComment();
        }
        catch { }
    }
    protected void grvResComment_RowEditing(object sender, GridViewEditEventArgs e)
    {
        grvResComment.EditIndex = e.NewEditIndex;
        GetAllResComment();
    }
    protected void grvResComment_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string i = Convert.ToString(grvResComment.DataKeys[e.RowIndex].Value);
        bool active = ((CheckBox)(grvResComment.Rows[e.RowIndex].Cells[3].Controls[0])).Checked;
        string rescomment = ((TextBox)(grvResComment.Rows[e.RowIndex].Cells[1].Controls[0])).Text;
        try {
            SQLHelper Dal = new SQLHelper();
            Dal.Open();
            Dal.CommandType = CommandType.StoredProcedure;
            Dal.CommandText = "SP_UpdateResComment";
            Dal.ParameterCollection = new string[3] { "@NoiDung", "@IsActive", "@BinhLuanID" };
            Dal.ValueCollection = new string[3] { rescomment,active.ToString(),i};
            Dal.ExecuteNonQuery();
            Dal.Close();
            Dal.Dispose();
            grvResComment.EditIndex = -1;
            GetAllResComment();
        }
        catch { }
    }
    protected void grvPostComment_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        grvPostComment.EditIndex = -1;
        GetAllPostComment();
    }
    protected void grvPostComment_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string i = Convert.ToString(grvPostComment.DataKeys[e.RowIndex].Value);
        //try
        //{
            SQLHelper Dal = new SQLHelper();
            Dal.Open();
            Dal.CommandType = CommandType.StoredProcedure;
            Dal.CommandText = "SP_DeletePostComment";
            Dal.ParameterCollection = new string[1] { "@BinhLuanID" };
            Dal.ValueCollection = new string[1] { i };
            Dal.ExecuteNonQuery();
            Dal.Close();
            Dal.Dispose();
            GetAllPostComment();
        //}
        //catch { }
    }
    protected void grvPostComment_RowEditing(object sender, GridViewEditEventArgs e)
    {
        grvPostComment.EditIndex = e.NewEditIndex;
        GetAllPostComment();
    }
    protected void grvPostComment_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string i = Convert.ToString(grvPostComment.DataKeys[e.RowIndex].Value);
        bool active = ((CheckBox)(grvPostComment.Rows[e.RowIndex].Cells[3].Controls[0])).Checked;
        string txtPostComment = ((TextBox)(grvPostComment.Rows[e.RowIndex].Cells[1].Controls[0])).Text;
        try
        {
            SQLHelper Dal = new SQLHelper();
            Dal.Open();
            Dal.CommandType = CommandType.StoredProcedure;
            Dal.CommandText = "SP_UpdatePostComment";
            Dal.ParameterCollection = new string[3] { "@NoiDung", "@IsActive", "@BinhLuanID" };
            Dal.ValueCollection = new string[3] { txtPostComment,active.ToString(), i };
            Dal.ExecuteNonQuery();
            Dal.Close();
            Dal.Dispose();
            grvPostComment.EditIndex = -1;
            GetAllPostComment();
        }
        catch { }
    }
    protected void ddlPost_SelectedIndexChanged(object sender, EventArgs e)
    {
        GetPostCommentByPostID();
    }

    protected void ddlRes_SelectedIndexChanged(object sender, EventArgs e)
    {
        GetResCommentByResID();
    }
    protected void grvResComment_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grvResComment.PageIndex = e.NewPageIndex;
        GetAllResComment();
    }
    protected void grvPostComment_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grvPostComment.PageIndex = e.NewPageIndex;
        GetAllPostComment();
    }
}
